package linkedlist.test725;

import listbyorder.utils.ListNode;

/**
 * @author code_yc
 * @version 1.0
 * @date 2020/7/15 12:42
 */
public class Solution1 {

    public ListNode[] splitListToParts(ListNode root, int k) {
        ListNode[] res = new ListNode[k];
        // 首先计算除链表的长度
        int l = 0;
        ListNode t = root;
        while (t != null) {
            t = t.next;
            l++;
        }
        int e = l / k;
        int n = l - (k * e);
        for (int i = 0; i < n; i++) {
            t = root;
            for (int j = 1; j < e + 1; j++) {
                t = t.next;
            }
            ListNode next = t.next;
            t.next = null;
            res[i] = root;
            root = next;
        }
        if (e == 0) return res;
        for (int i = n; i < k; i++) {
            t = root;
            for (int j = 1; j < e; j++) {
                t = t.next;
            }
            ListNode next = t.next;
            t.next = null;
            res[i] = root;
            root = next;
        }
        return res;
    }
}
